package com.ruoyi.system.mysql.mongodb;

import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/mongodb/users/nopermission")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping(value = "createUser")
    public User createUser(@RequestBody User user) {
        return userService.saveUser(user);
    }

    @PostMapping(value = "createUsernopermission")
    public User createUsernopermission(@RequestBody User user) {
        user.setId(UUID.randomUUID().toString());
        user.setName(UUID.randomUUID().toString());
        user.setEmail(UUID.randomUUID().toString());
        user.setAge(RandomUtils.nextInt());
        return userService.saveUser(user);
    }

    @GetMapping(value = "getAll")
    public List<User> getAll() {
        return userService.getAllUsers();
    }

    @GetMapping(value = "getById/{id}")
    public User getById(@PathVariable String id) {
        return userService.getUserById(id);
    }

    @GetMapping("/search")
    public List<User> byName(@RequestParam String name) {
        return userService.getUsersByName(name);
    }

    @DeleteMapping(value = "delete/{id}")
    public void delete(@PathVariable String id) {
        userService.deleteUser(id);
    }
}
